/**
 * @project name:crm
 */
package com.yunbo.crm.controller;

import com.yunbo.crm.base.BaseController;
import com.yunbo.crm.base.ResultInfo;
import com.yunbo.crm.model.UserModel;
import com.yunbo.crm.pojo.User;
import com.yunbo.crm.query.UserQuery;
import com.yunbo.crm.service.UserService;
import com.yunbo.crm.utils.LoginUserUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/19 23:42
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public ResultInfo userLogin(String userName, String userPwd) {
        ResultInfo resultInfo = new ResultInfo();
        UserModel userModel = userService.userLogin(userName, userPwd);
        resultInfo.setResult(userModel);
        return resultInfo;
    }

    @GetMapping("toPasswordPage")
    public ModelAndView toPassWordPage(ModelAndView m) {
        m.setViewName("user/password");
        return m;
    }

    @GetMapping("list")
    public Map<String, Object> userList(UserQuery userQuery) {
        return userService.queryByParamsForTable(userQuery);
    }


    @GetMapping("index")
    public ModelAndView goUserIndex(ModelAndView modelAndView) {
        modelAndView.setViewName("user/user");
        return modelAndView;
    }

    /**
     * 不要暴露用户行为
     *
     * @param request
     * @param userPwd
     * @param newPwd
     * @param repeatPwd
     * @return
     */
    @PutMapping
    public ResultInfo updateUserPwd(HttpServletRequest request, String userPwd, String newPwd, String repeatPwd) {
        ResultInfo resultInfo = new ResultInfo();
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        userService.updateUserPwd(userId, userPwd, newPwd, repeatPwd);
        return resultInfo;
    }

    @GetMapping("queryAllSales")
    public List<Map<String, Object>> queryAllSales() {
        List<Map<String, Object>> listContainMap = userService.queryAllSales();
        return listContainMap;
    }

    /**
     * 添加用户
     */
    @PostMapping("save")
    public ResultInfo saveUser(User user) {
        userService.saveUser(user);
        return success("用户添加成功");
    }

    @PutMapping("update")
    public ResultInfo updateUser(User user, HttpServletRequest request) {
        userService.updateUser(user, request);
        return success("用户信息更新成功");
    }

    @GetMapping("addOrUpdateUserPage")
    public ModelAndView addOrUpdateUserPage(ModelAndView modelAndView, User user) {
        if (!Objects.isNull(user.getRowId())) {
            user = userService.selectByPrimaryKey(user.getRowId());
            modelAndView.addObject("user", user);
        }
        modelAndView.setViewName("user/add_update");
        return modelAndView;
    }

    @DeleteMapping("delete")
    public ResultInfo deleteUser(Integer[] ids) {
        userService.deleteUserByIds(ids);
        return success("删除成功");
    }


}
